package com.example.composableexample.recomposable

import android.util.Log
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview


@Preview
@Composable
fun SnackDetail() {
    Log.d("SnackDetail", "SnackDetail: start")
    Box(modifier = Modifier.fillMaxSize()) {
        Log.d("SnackDetail", "Box: start")
        val titleState = remember { mutableStateOf("java") }
        // lambda 表达式知只是定义而未使用 所以并不会触发重组
        Title(title = { titleState.value }, titleState = titleState)
        Log.d("SnackDetail", "Box: end")
    }
    Log.d("SnackDetail", "SnackDetail: end")
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun Title(title: () -> String, titleState: MutableState<String>) {
    Log.d("SnackDetail", "Title: start")
    Column {
        // 2，状态使用
        Text(text = title())
        TextField(value = titleState.value, onValueChange = { titleState.value = it })
    }

    Log.d("SnackDetail", "Title: end")
}
